package com.xc.freeapp.account.controller;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.entity.AppImage;
import com.xc.freeapp.entity.Hospital;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.AppImageService;
import com.xc.freeapp.service.HospitalService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

@Controller
@RequestMapping("/appimage")
@Api(value = "/appimage" ,description="app图片")
public class AppImageController extends AbstractController {
	@Autowired
	private AppImageService appImageService;
	@Autowired
	private HospitalService hospitalService;
	
	/**
	/**
	 * 获取图片
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取appimage")
	public AppImage getAppImage(@RequestParam(value="name") String name,@RequestParam(required=false, value="versionno") String versionno)throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		List<AppImage> appImages = appImageService.getAppImageByname(authInfo.getAppIntId(), name,versionno);
		if(appImages==null||appImages.size()==0){
			if(!"关于我们".equals(name)){
				appImages =appImageService.getAppImageByname(-1, name,versionno);
			}else{
				
				appImages =appImageService.getAppImageByname(authInfo.getAppIntId(), "appicon",versionno);
				
			}
			
		}
		Hospital hospital = hospitalService.selectByPrimaryKey(authInfo.getAppIntId());
		appImages.get(0).setTotalphone(hospital.getTotalphone());
		for (AppImage appImage : appImages) {
			appImage.setUrl(UploadUtil.urlConversion(appImage.getUrl()));
		}
		return appImages.get(0);
	}
}
